package org.estela.ml.dataset;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;


/**
 *
 * @version 1.00
 * @date Apr 22, 2006
 * @author <a href="mailto:akantek@nyse.com">Antonio Kantek</a>
 */
public class DefaultDataSet extends AbstractDataSet {
	
	/** */
	private List<Instance> instances;

	/**
	 *
	 */
	public DefaultDataSet() {
		super();
	}
	
	/**
	 * 
	 * @param parser
	 * @throws Exception 
	 */
	public DefaultDataSet(DataSetParser parser) throws Exception {
		super(parser);
		
	}

	/*
	 *  (non-Javadoc)
	 * @see dataset.DataSet#addInstance(dataset.Instance)
	 */
	public void updateStats(Instance instance) {
		if (this.instances == null) {
			instances = new ArrayList<Instance>();
		}
		super.updateStats(instance); //update stats
		this.instances.add(instance);
	}	

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		InputStream in = new FileInputStream(args[0]);
		ArffParser parser = new ArffParser(in);
		
		DefaultDataSet dataSet = new DefaultDataSet(parser);
		System.out.println(dataSet);
	}

	@Override
	public String toString() {
		StringBuilder buff = new StringBuilder("[DefaultDataSet name=");
		buff.append(this.name).append(" , attributes=").append(Arrays.toString(this.attributes));
		buff.append(", frequencies=").append(Arrays.toString(this.frequencies));
		buff.append(", stats=").append(Arrays.toString(this.stats));
		buff.append(", missingCounts=").append(Arrays.toString(this.missingCounts));
		buff.append(", size()=").append(this.size());
		buff.append(']');
		return buff.toString();
	}

	/*
	 *  (non-Javadoc)
	 * @see dataset.DataSet#size()
	 */
	public int size() {
		return this.instances.size();
	}

	public Attribute getAttributeByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 *  (non-Javadoc)
	 * @see dataset.DataSet#iterator()
	 */
	public Iterator<Instance> iterator() {
		return this.instances.iterator();
	}

	public Iterator<Attribute> attributeIterator() {
		return new Iterator<Attribute>() {

			private int idx=0;
			
			public boolean hasNext() {
				return idx < attributes.length;
			}

			public Attribute next() {
				return attributes[idx++];
			}

			public void remove() {
				throw new Error("not implemented yet.");
			}
			
		};
	}

	public boolean containsAttribute(Attribute attribute) {
		for(int i=0;i<this.attributes.length;i++) {
			if (this.attributes[i].equals(attribute)) {
				return true;
			}
		}
		return false;
	}

	public Instance getInstance(int idx) {
		return this.instances.get(idx);
	}

}
